package account.provider;

import java.io.IOException;
import java.util.Properties;

import account.Account;
import aimee.common.Logger;
import aimee.common.SQLConnection;
import common.Crc32Until;

/**
 * 实现注册接口RegisterProvider
 * 
 * @author 毛建杰
 *
 */
public class RegisterProviderImpl implements RegisterProvider
{
	private static Properties properties;
	private static String jdbcDriver;

	static
	{
		try
		{
			properties = new Properties();
			properties.load(RegisterProviderImpl.class.getResourceAsStream("/account.properties"));
			jdbcDriver = "com.mysql.cj.jdbc.Driver";
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public int createIndexInfo(Account account)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			String sql = "insert into user_index (uname,regdate,regip,service,ad) value ('" + account.getUserName()
					+ "'," + account.getRegisterTime().getEpochSecond() + "," + Account.ip2Long(account.getRegisterIP())
					+ ",'gb','')";
			sqlConnection.executeUpdate(sql);
			sql = "select uid from user_index where uname='" + account.getUserName() + "'";
			return Integer.parseInt(sqlConnection.executeQuery(sql)[0][0]);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
		return -1;

	}

	@Override
	public void createUserInfo(Account account)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = (int) (Crc32Until.getValue(account.getUserName(), 64));
			String tableName = String.format("user_%02d", mod);
			String sql = "insert into " + tableName + " (uid,uname,pwd,salt) value (" + account.getUserID() + ",'"
					+ account.getUserName() + "','" + account.getPassword().getPassword() + "','"
					+ account.getPassword().getSalt() + "')";
			sqlConnection.executeUpdate(sql);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void createUserBaseInfo(Account account)
	{
		String url = properties.getProperty("mysql.account.url");
		String user = properties.getProperty("mysql.account.user");
		String password = properties.getProperty("mysql.account.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = account.getUserID() % 64;
			String tableName = String.format("user_base_%02d", mod);
			String sql = "insert into " + tableName + " (uid,uname,utype,ustatus,regdate,regip) value ("
					+ account.getUserID() + ",'" + account.getUserName() + "',0,1,"
					+ account.getRegisterTime().getEpochSecond() + "," + Account.ip2Long(account.getRegisterIP()) + ")";
			sqlConnection.executeUpdate(sql);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void createSecurityInfo(Account account)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = account.getUserID() % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "insert into " + tableName + " (uid,pass_level,idcard,realname,idcard_verify,wallow) value ("
					+ account.getUserID() + "," + account.getPassword().getLevel() + ",'" + account.getIdcard() + "','"
					+ account.getRealName() + "',2,0)";
			sqlConnection.executeUpdate(sql);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
	}
}
